﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace States
{
    public class MainMenu : BaseState
    {
        private static MainMenu theMainMenu;
        Systems.TextureManager theTextureManager;

        Vector2 PlayButtonPosition;
        Vector2 ExitButtonPosition;
        Color PlayButtonColor, ExitButtonColor;

        Rectangle PlayButtonRectangle, ExitButtonRectangle;
        private MainMenu()
        {
            PlayButtonPosition = new Vector2(600, 100);
            ExitButtonPosition = new Vector2(600, 400);
            PlayButtonColor = ExitButtonColor = Color.White;
        }

        public static MainMenu Instance
        {
            get
            {
                if (theMainMenu == null)
                    theMainMenu = new MainMenu();
                return theMainMenu;
            }
        }

        public override void Enter()
        {
            theDevice = TopDownShooter.Game1.deviceOfGraphicsCards;
            theContentManager = TopDownShooter.Game1.loadingManager;
            theSpriteBatch = TopDownShooter.Game1.spriteBatch;

            theTextureManager = new Systems.TextureManager(theContentManager);
            LoadGameTextures();
        }
        public void LoadGameTextures()
        {
            theTextureManager.AddTexture("PLAYBUTTON", "UI/PlayButton");
            theTextureManager.AddTexture("EXITBUTTON", "UI/ExitButton");

            PlayButtonRectangle = new Rectangle((int)PlayButtonPosition.X, (int)PlayButtonPosition.Y, theTextureManager.GetTexture("PLAYBUTTON").Width, theTextureManager.GetTexture("PLAYBUTTON").Height);
            ExitButtonRectangle = new Rectangle((int)ExitButtonPosition.X, (int)ExitButtonPosition.Y, theTextureManager.GetTexture("EXITBUTTON").Width, theTextureManager.GetTexture("EXITBUTTON").Height);     
        }
        public override void Exit()
        {
            theTextureManager.ShutdownTextureManager();
        }

        public override void GetInput(float ElapsedTime)
        {
            if (PlayButtonRectangle.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
            {
                PlayButtonColor = Color.Red;
                if (Mouse.GetState().LeftButton == ButtonState.Pressed)
                    TopDownShooter.Game1.ChangeState(States.Gameplay.Instance);
            }
            else
                PlayButtonColor = Color.White;

            if (ExitButtonRectangle.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
            {
                ExitButtonColor = Color.Red;
                if(Mouse.GetState().LeftButton == ButtonState.Pressed)
                    TopDownShooter.Game1.exitFlag = true;
            }
            else
                ExitButtonColor = Color.White;
            
            

        }

        public override void Update(float ElapsedTime)
        {
            GetInput(ElapsedTime);
        }
        public override void Render(float ElapsedTime)
        {
            theDevice.Clear(Color.White);
            theSpriteBatch.Begin();

            theSpriteBatch.Draw(theTextureManager.GetTexture("PLAYBUTTON"), PlayButtonPosition, PlayButtonColor);
            theSpriteBatch.Draw(theTextureManager.GetTexture("EXITBUTTON"), ExitButtonPosition, ExitButtonColor);
            theSpriteBatch.End();
        }
    }
}